package com.zhongge.singlelistOJ;

/**
 * @ClassName Solution
 * @Description TODO 反转链表
 * @Author 笨忠
 * @Date 2025/8/9 21:04
 * @Version 1.0
 */
public class Solution {
    public static ListNode reverseList(ListNode head) {
        //处理首节点
        ListNode cur = head;
        cur = cur.next;
        head.next = null;

        //循环头插
        while (cur != null) {
            //你等我
            ListNode curN = cur.next;

            //我处理它(头插)
            cur.next = head;
            head = cur;

            //我再找你
            cur = curN;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        ListNode listNode = reverseList(node1);
        ListNode prev = listNode;
        while (prev != null) {
            System.out.print(prev.val + " ");
            prev = prev.next;
        }
    }
}
